﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using GalaSoft.MvvmLight.Messaging;
using ShoppingList.ViewModel;
using WindowsPhoneRecipes;

namespace ShoppingList.Views
{
    public partial class ShoppingsView : PhoneApplicationPage
    {
        public ShoppingsView()
        {
            InitializeComponent();

          

            Storyboard sb = Helpers.VisualStateManagerUtils.FindStoryboard(this.LayoutRoot, "VisualStateGroup", "ShowAviableProducts");

            sb.Completed += new EventHandler(sb_Completed);
            Messenger.Default.Register<string>(this, "SHOPPINGLIST_TOKEN",
         s =>
         {
             if (s == "newitem")
             {
                 NavigationService.Navigate(new Uri("/Views/NewItemShoppingProduct.xaml", UriKind.Relative));
             }

            
         }
         );

            this.Loaded += new RoutedEventHandler(ShoppingsView_Loaded);
        }

        void ShoppingsView_Loaded(object sender, RoutedEventArgs e)
        {
           // ViewModelLocator.CreateViewModelShopping();
             
            ViewModelLocator.ViewModelShoppingStatic.MyShopItem = null;
            //this.listBox1.SelectedItem = null;
           // VisualStateManager.GoToState(this, "ShowShoppingList", true);
          //  throw new NotImplementedException();
        }

        void sb_Completed(object sender, EventArgs e)
        {
            Messenger.Default.Send<string>("LoadItems", "PRODUCTITEMS_TOKEN");
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);

            // due to bug, BackKey doesnt send navigation events so we handle this myself
            if (NavigationService.CanGoBack)
            {
                e.Cancel = true;
                NavigationService.GoBack();
            }

            
          
           // this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {

            if (NonLinearNavigationService.Instance.IsRecursiveBackNavigation == true)
            {
               // WindowsPhoneRecipes.Logger.Instance.AddLine("IsRecursiveBackNavigation = true");
                return;
            }
            //if (ViewModelLocator.ViewModelShoppingStatic != null)
            //{
            //    VisualStateManager.GoToState(this, "ShowShoppingList", true);
            //    ViewModelLocator.ClearViewModelShopping();
            //    ViewModelLocator.CreateViewModelShopping();
            //} 

            VisualStateManager.GoToState(this, "ShowShoppingList", true);
            listBox1.SelectedItem = null;
            
           
            
        }
    }
}